package com.workerhelping.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.workerhelping.po.Notice;
import com.workerhelping.result.Result;
import com.workerhelping.service.NoticeService;
import com.workerhelping.vo.SubmitNoticeVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;


    /**
     * 查询id title
     *
     * @param
     * @return
     */
//    @Deprecated
//    @GetMapping(value = "/search1")
//    public Result<List<Notice>> search() {
//        try {
//            List<Notice> notices = noticeService.listNotice();
//            return Result.ok(notices);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return Result.fail();
//        }
//    }

    /**
     * 查询档案简略信息
     *  - id
     *  - title
     *  - create_date
     * @return
     */
    @GetMapping(value = "/search")
    public Result<List<Notice>> search() {
        try {
            List<Notice> notices = noticeService.listNotice();
            return Result.ok(notices);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }



    /**
     * 根据关键词查询
     * @param keyword
     * @return
     */
    @GetMapping(value = "/search/{keyword}")
    public Result<List<Notice>> searchByKeyword(@PathVariable("keyword") String keyword) {
        try {
            List<Notice> notices = noticeService.list(new QueryWrapper<Notice>().select("id","title","create_date").like("title", keyword));
            return Result.ok(notices);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    /**
     * 添加政策信息
     *
     * @param noticeVO
     * @return
     */
    @PostMapping("/add")
    public Result<String> add(@RequestBody SubmitNoticeVO noticeVO) {
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeVO, notice);
        notice.setCreateDate(new Date());
        try {
            boolean save = noticeService.save(notice);
            if (!save)
                return Result.fail();
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    /**
     * 通过id获取政策
     *
     * @param id
     * @return
     */
    @GetMapping("/getNoticeById/{id}")
    public Result<Notice> getNoticeById(@PathVariable("id") Integer id) {
        try {
            Notice notice = noticeService.getById(id);
            return Result.ok(notice);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    /**
     * 删除政策信息
     *
     * @param id
     * @return
     */
    @PostMapping("/deleteNoticeById/{id}")
    public Result<String> deleteNoticeById(@PathVariable("id") Integer id) {
        try {
            boolean isRemove = noticeService.removeById(id);
            if (!isRemove)
                return Result.fail();
            return Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }
}
